﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Documents;
using System.Windows.Media.Imaging;
using Heyo.Class.Helper;
using Heyo.Class.Helper.Encryption;
using Heyo.Class.Helper.Network;
using Heyo.Launcher.Models.Data.Game;
using HeyoUi.Models.Data.GameData;
using HeyoUi.Util;
using KMCCC.Launcher;
using Newtonsoft.Json;

namespace HeyoUi.Models.Data.Game
{
    /// <summary>
    ///     本地游戏列表数据模型
    /// </summary>
    public class LocalGameInfo : GameInfo
    {
        public LocalGameInfo(KMCCC.Launcher.Version version)
        {
            Name = version.Id;
            Version = version.JarId;
            KmcccVersion = version;

            string gameInfoJson = GameLocator.GetGameInfoJsonPath(version.Id);
            HeyoGameJson heyoGameJson;
            if (File.Exists(gameInfoJson))
            {
                FileStream stream = File.Open(gameInfoJson, FileMode.Open);
                StreamReader reader = new StreamReader(stream); heyoGameJson = JsonConvert.DeserializeObject<HeyoGameJson>(reader.ReadToEnd());
                stream.Close();
                reader.Dispose();
            }
            else
            {
                heyoGameJson = new HeyoGameJson();
                FileStream stream = File.Open(gameInfoJson, FileMode.Create);
                StreamWriter writer = new StreamWriter(stream);
                writer.Write(JsonConvert.SerializeObject(heyoGameJson,Formatting.Indented));
                writer.Dispose();
                stream.Close();
            }
            if (!string.IsNullOrWhiteSpace(heyoGameJson.IconData))
            {
                try
                {
                    Icon = Base64Helper.GetDataFromString(heyoGameJson.IconData).ToBitmapImage();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
#if DEBUG
                    throw;
#endif
                }
            }

            HasPlayedTime = new GameTime(heyoGameJson.PlayedTime);
            LastRunTime = new LastRunDate(heyoGameJson.LastRunTime);

        }
        public KMCCC.Launcher.Version KmcccVersion { get; }

        /// <summary>
        /// 图标
        /// </summary>
        public BitmapSource Icon { get; set; } = new BitmapImage(new Uri("pack://application:,,,/HeyoUi;component/Resources/minecraft.png"));
        /// <summary>
        ///     游戏名称
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        ///     材质包名称
        /// </summary>
        public IEnumerable<ResPack> ResPack { get; set; }

        //public IEnumerable<ScreenShot> ScreenShotUrl { get; set; }
        /// <summary>
        ///     光影包路径
        /// </summary>
        public IEnumerable<string> ShaderPackUrl { get; set; }

        /// <summary>
        ///     Mod路径
        /// </summary>
        public IEnumerable<string> ModUrl { get; set; }

        /// <summary>
        ///     存档路径
        /// </summary>
        public IEnumerable<string> SavesUrl { get; set; }

        /// <summary>
        ///     是否选择
        /// </summary>
        public bool IsChecked { get; set; }

        /// <summary>
        /// 上次运行时间
        /// </summary>
        public LastRunDate LastRunTime { get; set; } = new LastRunDate(DateTime.Now);

        /// <summary>
        /// 分钟
        /// </summary>
        public GameTime HasPlayedTime { get; set; } = new GameTime(25);

        public struct LastRunDate
        {
            public DateTime Date { get; set; }
            public LastRunDate(DateTime date)
            {
                Date = date;
            }
            public override string ToString()
            {
                return Date.ToString("yy年MM月dd日");
            }
        }
        public struct GameTime
        {
            public int Minute { get; set; }

            public GameTime(int minute)
            {
                Minute = minute;
            }
            public override string ToString()
            {
                return Minute.ToString() + "分钟";
            }
        }
    }
}